package com.cby.widget.sample


import android.os.Bundle
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.cby.widget.lib.kotlin.*

/**
 * @description: MainAdapter
 *
 * @author ChenBaoyang
 * @date 2021/10/8 13:59
 */
class MainAdapter : RecyclerView.Adapter<MainAdapter.MainViewHolder>() {

    private val data = mutableListOf<MainBean>()

    inner class MainViewHolder(itemView: View, private val title: TextView) : RecyclerView.ViewHolder(itemView) {

        fun bindData(bean: MainBean) {
            title.text = bean.fragmentTitle
            itemView.setOnClickListener {
                val data = Bundle()
                data.putString(CommonActivity.keyName, bean.fragmentName)
                data.putString(CommonActivity.keyTitle, bean.fragmentTitle)
                it.context.start(CommonActivity::class.java, data)
            }
        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MainViewHolder {
        var title: TextView? = null
        val itemView = parent.context.run {
            LinearLayout {
                layout_width = match_parent
                layout_height = 40
                orientation = vertical
                margin_top = 5
                margin_bottom = 5
                title = TextView {
                    layout_width = match_parent
                    layout_height = match_parent
                    gravity = gravity_center
                    background_drawable_state_list = listOf(
                        intArrayOf(state_unpressed) to shape {
                            solid_color = "#FF03DAC5"
                            corner_radius = 20
                        },
                        intArrayOf(state_pressed) to shape {
                            solid_color = "#FF018786"
                            corner_radius = 20
                        }
                    )
                }
            }
        }

        return MainViewHolder(itemView, title!!)
    }

    override fun onBindViewHolder(holder: MainViewHolder, position: Int) {
        holder.bindData(data[position])
    }

    override fun getItemCount() = data.size

    fun addData(data: MutableList<MainBean>) {
        this.data.clear()
        this.data.addAll(data)
    }
}

data class MainBean(
    val fragmentTitle: String,
    val fragmentName: String
)